<?php

$page = "Schedule";

include_once("common.php");


gpp_print_header($page, "");

// If the user is logged in, we want to send them to the page where
// they can actually use the application.
if(!gpp_is_authenticated())
{
	gpp_redirect("entrance");
}

$acronyms = get_acronyms_for_display();

?>

<script type="text/javascript">

$(document).ready(function(){

	reset_stored_schedule();
    $('#search_box').hide();
});



$(function() {
	$(".save_schedule").click(function(){
		var answer = confirm("Are you sure you wish to attempt to save this as your schedule?");
		if(answer)
		{
			save_schedule();
		}
	})
})

$(function() {
	$(".search_clear").click(function(){
		//$("#search_box").modal();
        $("#search_results tbody").html("");
	})
})

$(function() {
	$("#search_send").click(function(){
		terms = $("#search_terms").val();
		major = $("#search_major").val();
		
		if(major == "Choose")
		{
		    alert("You must choose a major to search in.");
		    return;
		}
		
		
		$.getJSON("/json_utils.php",{task: 'perform_search', search_terms: terms, search_major: major}, function(j){
		            
            table_value = "";
            
            $("#search_results tbody").html("");
            var course_array = j;
              for(i in course_array)
              {  
                table_value += CourseFromJSON(course_array[i]).display_course_search();
              }
            $("#search_results tbody").append(table_value);
            
        });
		

	})
})

$(function() {
	$("#remaining_requirements").click(function(){	
		$.getJSON("/json_utils.php",{task: 'get_remaining_requirements'}, function(j){
		            
            table_value = "";
            
            $("#search_results tbody").html("");
            
            var course_array = j;
              for(i in course_array)
              {  
                table_value += CourseFromJSON(course_array[i]).display_course_search();
              }
            $("#search_results tbody").append(table_value);
            
        });
		

	})
})




$(function() {
	$("#add_class_search").click(function(){
        
        $(".search_checkbox").each(function() {
            semester = $("#search_add_semester").val();
            
            if($(this).attr('checked'))
            {
                course = JSON.parse($(this).attr('value'));
                add_course_to_semester("" + semester, course);
            }
            
        })        
    })

})
       
$(function() {
	$(".reset_schedule").click(function() {
		var answer = confirm("Are you sure you wish to reset your schedule to the stored one?");
		if(answer)
		{
			reset_stored_schedule();
		}
	})
})

$(function() {
	$(".generate_schedule").click(function() {
		var answer = confirm("WARNING. Generating a schedule clears out your current schedule. Continue?");
		if(answer)
		{
			generate_schedule();
		}
	})
})

$(function() {
    $(".toggle_search").click(function(){
     $('#search_box').toggle();
   })

})


</script>


<p class="noprint">To edit your future schedule info, use the fields below.</p>

<h2><?php echo $message ?></h2>

<div id="schedule_form">
        <div class="noprint">
            <input type="button" class="save_schedule" value="Save Schedule" />
            <input type="button" value="Print This Page" onclick="window.print();return false;" />
            <input type="button" class="generate_schedule" value="Generate Schedule" />
            <input type="button" class="reset_schedule" value="Reset Schedule" />
            <input type="button" class="toggle_search" value="Show/Hide Search & Remaining Requirements" />
        </div>
        <div id="search_box" class="noprint">
            <form>
                <label for="search_major">Select a major to search:</label>
                <select class="acronym" id="search_major">
                    <?php echo $acronyms; ?>
                </select><br />
                <label for="search_terms">Input keywords to search:</label>
                <input id="search_terms" type="text" /><br />
                <input type="button" id="search_send" value="Perform Search" />
                <input type="button" id="remaining_requirements" value="List Remaining Requirements" />
                <input type="button" class="search_clear" value="Clear Search" />
                
                <table id="search_results">
                    <thead><tr class="semester_cap"><td style="width:400px">Course</td><td style="width:50px">Credits</td><td>&nbsp;</td>
                    </tr></thead>
                    <tbody></tbody>
                    
                    <tfoot class="noprint"><tr><td>Add Selected Courses to Semester: 
                                <select id="search_add_semester">
        <?php for($i = 0; $i < $_SESSION['semesters_left']; $i++)
        {
                                echo sprintf('<option value="%d">%s</option>', $i, get_semester_name($i));
        } ?>
                                </select></td><td>&nbsp;</td>
                    <td><input type="button" size="1" value="+" name="add_class_search" id="add_class_search" />
                    </td></tr></tfoot>
                </table>
                
                 <input type="button" class="toggle_search" value="Show/Hide Search & Remaining Requirements" />

            </form>
            <br />
            
            
            
	    </div>

        
<table>
<?php

// Here, we get the number of semesters the student has left.



for($i = 0; $i < $_SESSION['semesters_left']; $i++)
{
?>
<?php 
    if(($i % 2) == 0) 
        echo '<tr valign="top"><td>';
    else
        echo '<td>';
    ?>

    <h3><?php echo get_semester_name($i) ?></h3>
	<table semester="s<?php echo $i ?>" id="semester_s<?php echo $i ?>" class="semester_list"><thead><tr class="semester_cap"><td style="width:400px">Course</td><td style="width:50px">Credits</td><td style="width:50px">&nbsp;</td></tr></thead>
	<tbody>
	<!-- Put in original courses here, but they need to be loaded into a JavaScript object and put here
		automagically -->
	
	</tbody>
	<tfoot class="noprint">	<tr><td>Add Course: <select class="acronym" title="<?php echo $i ?>">
				<?php echo $acronyms; ?>
			</select>
			<select id="numbers<?php echo $i ?>"></select></td><td>&nbsp;</td>
			<td><input type="button" size="1" title="<?php echo $i ?>" value="+" class="add_class" name="add_class_<?php echo $i ?>" id="add_class_<?php echo $i ?>" />
            </td></tr></tfoot>
	</table>
 <?php 
    if(($i % 2) == 0) 
        echo '</td>';
    else
        echo '</td></tr>';
    
    ?>
<?php } ?>
</table>

<div class="noprint"><input type="button" class="save_schedule" value="Save Schedule" /></div>

<?php 

gpp_print_footer();

?>